<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak={$data.ak}"></script>
<style>
    html, body, #allmap{
        width: 100%;
        height: 100%;
        padding: 0;
        margin: 0;
        overflow: hidden;
    }
</style>
<div class="layui-form-item">
    <label class="layui-form-label">搜索</label>
    <div class="layui-input-block">
        <input type="text" name="keywords" id="suggestId" class="layui-input" placeholder="地址搜索" value="">
        <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
    </div>
</div>
<div id='allmap'></div>
<script>
   var init_param = {
       lon:{$data.lon|default=0},
       lat:{$data.lat|default=0},
       id:{$data.id|default=0},
       addr_detail:'{$data.addr_detail}',
       ak:'{$data.ak}',
       input:'{$data.input|default=""}',
   }
   var Controller = {
       build: function () {
           // 百度地图API功能
           var map = baidu_map.init(init_param.id,init_param.lon,init_param.lat,init_param.addr_detail);
           map.location();
       },
   };
   var baidu_map={
       param:{
           lon:116.331398,
           lat:39.897445,
           level:11,
           map:{},
           id:'allmap',
           addComp:{},
           province:"",
           city:"",
           area:"",
           sreach:'',
           business:'',
           button_id:0,
       },
       init:function (id,lon,lat,sreach,button_id,level){
           id && (this.param.id =id);
           button_id && (this.param.button_id =button_id);
           lon && (this.param.lon =lon);
           lat && (this.param.lat =lat);
           level && (this.param.level =level);
           sreach && (this.param.sreach =sreach);
           this.param.map = new BMap.Map(this.param.id);
           this.param.map.centerAndZoom(new BMap.Point(this.param.lon, this.param.lat), this.param.level);
           this.param.map.enableScrollWheelZoom(true);
           var ac = new BMap.Autocomplete(
               {"input" : "suggestId"
                   ,"location" : this.param.map
               });
           ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件

           });
           ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
               var _value = e.item.value;
               baidu_map.param.business =  _value.business;
               baidu_map.param.sreach = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
               baidu_map.sreach();
           });

           return this;
       },
       location:function (lon,lat) {
           lon && (this.param.lon =lon);
           lat && (this.param.lat =lat);
           this.param.map.clearOverlays(); //清理下标
           var new_point = new BMap.Point(this.param.lon, this.param.lat);
           var marker = new BMap.Marker(new_point);  // 创建标注
           this.param.map.addOverlay(marker);              // 将标注添加到地图中
           this.param.map.panTo(new_point);
           var gc =  new BMap.Geocoder();
           gc.getLocation(new_point, function (rs) {
               console.log(rs);
               baidu_map.param.addComp = rs.addressComponents;
               baidu_map.param.province = baidu_map.param.addComp.province;
               baidu_map.param.city = baidu_map.param.addComp.city;
               baidu_map.param.area = baidu_map.param.addComp.district;
               baidu_map.param.sreach = baidu_map.param.province +  baidu_map.param.city +  baidu_map.param.addComp.district +  baidu_map.param.addComp.street;
               baidu_map.show(baidu_map.param,new_point);
               baidu_map.setData(baidu_map.param);
           });
       },
       listen:function (event,param,callBack) {
           switch (event)
           {
               case 'click': //地图单机
                   this.click(callBack);
                   break;

           }
       },
       click:function (callBack) {
           this.param.map.addEventListener('click', function (e) {
               //触发定位
               baidu_map.location(e.point.lng,e.point.lat);
               // alert('点击位置经纬度：' + e.latlng.lng + ',' + e.latlng.lat);
               var gc =  new BMap.Geocoder();
               gc.getLocation(e.point, function (rs) {
                   console.log(rs);
                   baidu_map.param.addComp = rs.addressComponents;
                   baidu_map.param.province = baidu_map.param.addComp.province;
                   baidu_map.param.city = baidu_map.param.addComp.city;
                   baidu_map.param.area = baidu_map.param.addComp.district;
                   callBack && (callBack(baidu_map.param));
                   baidu_map.setData(baidu_map.param);
                   baidu_map.show(baidu_map.param,e.point);
               });
           });
       },
       show:function (param,point) {
           console.log(param);
           var opts = {
               width : 200,     // 信息窗口宽度
               // height: 100,     // 信息窗口高度
               title : param.area , // 信息窗口标题
               message:param.area
           }
           var infoWindow = new BMap.InfoWindow("地址："+param.addComp.province + ", " + param.addComp.city + ", " + param.addComp.district + " " + param.addComp.street + " " + param. addComp.streetNumber + " "+param.sreach, opts);  // 创建信息窗口对象
           this.param.map.openInfoWindow(infoWindow, point); //开启信息窗口
       },
       sreach:function () {
           function myFun(){
               var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
               baidu_map.location(pp.lng,pp.lat)
           }
           var local = new BMap.LocalSearch(baidu_map.param.map, { //智能搜索
               onSearchComplete: myFun
           });
           local.search(baidu_map.param.sreach);
       },
       setData:function (param) {
           param && (this.param =param);
           console.log(this.param.id);
           // this.getCityCode(this.param.lon,this.param.lat,function (data) {
           //     console.log(data);
           // })
           //设置属性
           if(this.param.button_id){
               parent.$("#"+this.param.button_id).attr( "data-lon"  ,this.param.lon);
               parent.$("#"+this.param.button_id).attr( "data-lat"  ,this.param.lat);
               parent.$("#"+this.param.button_id).attr( "data-addr"  ,this.param.sreach + this.param.addComp.street + this.param.addComp.streetNumber);
               parent.$("#"+this.param.button_id).attr( "data-city"  ,this.param.city);
               parent.$("#"+this.param.button_id).attr( "data-prov"  ,this.param.province);
               parent.$("#"+this.param.button_id).attr( "data-area"  ,this.param.area);
               parent.$("#"+this.param.button_id).attr( "data-stre"  ,this.param.addComp.street);
               parent.$("#"+this.param.button_id).attr( "data-stre_num"  ,this.param.addComp.streetNumber);
           }else{
               parent.$(".map-location").attr( "data-lon"  ,this.param.lon);
               parent.$(".map-location").attr( "data-lat"  ,this.param.lat);
               parent.$(".map-location").attr( "data-addr"  ,this.param.sreach + this.param.addComp.street + this.param.addComp.streetNumber);
               parent.$(".map-location").attr( "data-city"  ,this.param.city);
               parent.$(".map-location").attr( "data-prov"  ,this.param.province);
               parent.$(".map-location").attr( "data-area"  ,this.param.area);
               parent.$(".map-location").attr( "data-stre"  ,this.param.addComp.street);
               parent.$(".map-location").attr( "data-stre_num"  ,this.param.addComp.streetNumber);
           }
           if(init_param.input){
               parent.$("#"+init_param.input).attr( "data-lon"  ,this.param.lon);
               parent.$("#"+init_param.input).attr( "data-lat"  ,this.param.lat);
               parent.$("#"+init_param.input).attr( "data-addr"  ,this.param.sreach + this.param.addComp.street + this.param.addComp.streetNumber);
               parent.$("#"+init_param.input).attr( "data-city"  ,this.param.city);
               parent.$("#"+init_param.input).attr( "data-prov"  ,this.param.province);
               parent.$("#"+init_param.input).attr( "data-area"  ,this.param.area);
               parent.$("#"+init_param.input).attr( "data-stre"  ,this.param.addComp.street);
               parent.$("#"+init_param.input).attr( "data-stre_num"  ,this.param.addComp.streetNumber);
               parent.$("#"+init_param.input).val(this.param.sreach + this.param.addComp.street + this.param.addComp.streetNumber);
           }

       },
       getCityCode:function (lon,lat,callBack) {
           parent.$.ajax({
               url: '/manage/common/cityInfo?ak=CSsWj7ucuVqAEaFPtTsGrrFUcSFwvm3O&lon='+lon+'&lat='+lat,
               type: "get",
               dataType: 'json',
               success: function(data){
                   callBack(data);
               }
           });
       }

   }
   Controller.build();
</script>